$(function () {

    var form = new xhForm($('#xh_form'));
    form.parse();

    $('[name=iProvince],[name=iCity],[name=iCounty],[name=iCompRun]').select2();
    fillSelect(province,'iProvince',0);
    $('#iSecArea').on('change',function () {
        var val = $(this).val();
        if(val != ''){
            xhExtra.ajaxRequest('lm/rent/findArea/'+val,{tag:'all'},{},function (res) {
                var control = $('#iThirdArea');
                control.empty();//清空下拉框
                control.append("<option value='' style='display: none;' selected>请选择</option>");
                $.each(res , function (key,val) {
                    control.append("<option value='" + key + "'>" + val + "</option>");
                });
                if(info['iThirdArea'] != '' && info['iThirdArea'] != undefined){
                    control.val(info['iThirdArea']);
                }
            })
        }
    });

    form.fill(info);
    $('#iSecArea').val(info['iSecArea']).trigger('change');
    $('#iProvince').on('change',function () {
        var provinceId =  $(this).val();
        fillSelect(city,'iCity',provinceId);
        $('[name=sProvince]').val(province[provinceId].areaName);
    });
    $('#iCity').on('change',function () {
        var cityId =  $(this).val();
        fillSelect(county,'iCounty',cityId);
        $('[name=sCity]').val(city[cityId].areaName);
    });
    $('#iCounty').on('change',function () {
        var countyId =  $(this).val();
        $('[name=sCounty]').val(county[countyId].areaName);
    });

    $('[name=iCompRun]').val(info['iCompRun']).trigger('change');

    if(info['sProvince']!=''){
        $.each(province,function (i,item) {
            if(item['areaName'] == info['sProvince']) {
                $('#iProvince').val(item['areaCode']).trigger('change');
            }
        });
    }
    if(info['sCity']!=''){
        $.each(city,function (i,item) {
            if(item['areaName'] == info['sCity']) {
                $('#iCity').val(item['areaCode']).trigger('change');
            }
        });
    }
    if(info['sCounty']!=''){
        $.each(county,function (i,item) {
            if(item['areaName'] == info['sCounty']) {
                $('#iCounty').val(item['areaCode']).trigger('change');
            }
        });
    }





    $('#save').click(function () {
        window.top.swal({
            title:  '确认保存么？',
            showCancelButton: true,
            cancelButtonText: '取消',
            confirmButtonText: '确认',
            allowOutsideClick: true,
            showCloseButton: true,
            showLoaderOnConfirm: true,
            type: 'warning',
            preConfirm: function() {
                return new Promise(function (resolve, reject) {
                    form.submit('lm/rent/saveInfo',{},function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                        // location.reload();
                    })
                })
            }
        }).then(function(isConfirm) {}, function(dismiss) {});
    });







    function fillSelect(data,selector,pid) {
        var control = $('#'+selector);
        control.empty();//清空下拉框
        control.append("<option value='' style='display: none;' selected>请选择</option>");
        $.each(data, function (id,item) {
            if(pid==null){
                control.append("<option value='" + item['areaCode'] + "'>" + item['areaName'] + "</option>");
            }else if(pid == item['parentCode']){
                control.append("<option value='" + item['areaCode'] + "'>" + item['areaName'] + "</option>");
            }

        });
    }
});